package com.mDigitalHealth.http;

import java.util.ArrayList;
import java.util.List;

import org.apache.http.HttpResponse;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.util.EntityUtils;

public class HttpMDigitalHealth {

	private String executeHttpPost_another(String strParamName, String strParamValue) {
		return null;
	}

	private String executeHttpPost(String strURL, String strParamName, String strParamValue) {
		try {
			DefaultHttpClient httpClient = new DefaultHttpClient();
			HttpPost postMethod = new HttpPost(strURL);

			List params = new ArrayList();
			params.add(new BasicNameValuePair(strParamName, strParamValue));

			postMethod.setEntity(new UrlEncodedFormEntity(params, "UTF-8"));
			HttpResponse response = httpClient.execute(postMethod);
			String strResponse = EntityUtils.toString(response.getEntity());
			httpClient.getConnectionManager().shutdown();
			return strResponse;
		} catch (Exception e) {
		}
		return null;
	}

	public DigitalHealthUserLoginResponse DigitalHealthUserLogin(String strURL,
			DigitalHealthUserLoginRequest loginReq) {
		try {
			if (loginReq == null)
				return null;
			String strHttpResponse = null;
			String jsonText = loginReq.encode();
			DigitalHealthUserLoginResponse loginRes = new DigitalHealthUserLoginResponse();

			strHttpResponse = executeHttpPost(strURL, "UserLoginRequest", jsonText);

			int nResult = loginRes.decode(strHttpResponse);

			return loginRes;
		} catch (Exception e) {
		}
		return null;
	}

	public DigitalHealthUploadStepInfoResponse DigitalHealthUploadStepInfo(String strURL,
			DigitalHealthUploadStepInfoRequest stepInfoReq) {
		try {
			if (stepInfoReq == null)
				return null;
			DigitalHealthUploadStepInfoResponse res = new DigitalHealthUploadStepInfoResponse();
			String strHttpResponse = null;

			String jsonText = stepInfoReq.encode();
			strHttpResponse = executeHttpPost(strURL, "UploadStepInfoRequest", jsonText);

			int nResult = res.decode(strHttpResponse);

			return res;
		} catch (Exception e) {
		}
		return null;
	}

	public DigitalHealthFetchUserInfoResponse DigitalHealthFetchUserInfo(String strURL,
			DigitalHealthFetchUserInfoRequest req) {
		try {
			if (req == null)
				return null;
			DigitalHealthFetchUserInfoResponse res = new DigitalHealthFetchUserInfoResponse();
			String strHttpResponse = null;

			String jsonText = req.encode();
			strHttpResponse = executeHttpPost(strURL, "FetchUserInfoRequest", jsonText);

			int nResult = res.decode(strHttpResponse);

			return res;
		} catch (Exception e) {
		}
		return null;
	}

	public DigitalHealthUploadUserInfoResponse DigitalHealthUploadUserInfo(String strURL,
			DigitalHealthUploadUserInfoRequest req) {
		try {
			if (req == null)
				return null;
			DigitalHealthUploadUserInfoResponse res = new DigitalHealthUploadUserInfoResponse();
			String strHttpResponse = null;

			String jsonText = req.encode();
			strHttpResponse = executeHttpPost(strURL, "UploadUserInfoRequest", jsonText);
			int nResult = res.decode(strHttpResponse);

			return res;
		} catch (Exception e) {
		}
		return null;
	}

	public RegResponse Reg(String strURL, RegRequest req) {
		try {
			if (req == null)
				return null;
			RegResponse res = new RegResponse();
			String strHttpResponse = null;

			String jsonText = req.encode();
			strHttpResponse = executeHttpPost(strURL, "UserRegisterRequest", jsonText);

			int nResult = res.decode(strHttpResponse);

			return res;
		} catch (Exception ex) {

			return null;
		}
	}
}
